﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseAccessLayer;
using System.Net.Mail;
using System.Configuration;
using SavoryBL;

namespace WebApplication.administrator
{
    public partial class Request_Password : System.Web.UI.Page
    {
        SavoryAdminAccount adminAccountObj = new SavoryAdminAccount();
        protected void btn_Submit_Click(object sender, EventArgs e)
        {
            try
            {
                string Password = adminAccountObj.GetAdminPasswordByEmailID(txt_EmailID.Text.Trim());
                if (Password != "")
                {
                    bool mailsent = SendMail(txt_EmailID.Text.Trim(),Password);
                    if (mailsent == true)
                    {
                        div_PwdMailMsg.Style["display"] = "block";
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
        public Boolean SendMail(string emailID,string pwd)
        {
            String subject = String.Empty;
            string sbMessageBody = "";
            Boolean mailSent = false;

            try
            {
                subject = "Password Details.";
                sbMessageBody += "<center>";
                sbMessageBody += "<table border='0' cellpadding='0' cellspacing='0' height='100%' width='640px' style='font-size:12px;font-family:Arial;'>";
                sbMessageBody += "<tr>";
                sbMessageBody += "<td align='center' valign='top' style='padding:10px; border:5px solid #036838'><table cellpadding='0' cellspacing='0' width='100%'>";
                sbMessageBody += "<tr><td align='left'>";
                sbMessageBody += "<a href='http://realestate-contracting.com'><img style='border:none' alt='' src='http://realestate-contracting.com/images/savory-logo.png'/></a></td></tr><tr>";
                sbMessageBody += "<td style='border-top:1px solid #e1e1e1; vertical-align:text-top; padding:10px 0 0 0;'><table cellpadding='0' cellspacing='0' width='100%'>";
                sbMessageBody += "<tr><td align='left' style='padding:0 0 10px 0;'><b>Thank you for  showing interest in Savory Housing.</td></tr>";
                sbMessageBody += "<tr><td align='left' style='padding:0 0 10px 0;'>Your Password is <b>"+pwd+"</td></tr>";
                sbMessageBody += "<tr><td>If you feel that you are not the recipient of this email then please tell us <a style='color:#036838;' href='mailto:info@realestate-cntracting.com'><b>report</b></a>.</td></tr></table></td></tr></table>";
                sbMessageBody += "</td></tr><tr><td><table  cellpadding='0' cellspacing='0' width='100%' style='margin:0 0 10px 0; line-height:18px;'><tr>";
                sbMessageBody += "<td style='text-align:left;padding:5px;color:#363636;vertical-align:top;'>Copyright © 2010-2015 <a style='text-decoration:none; color:#036838;' href='http://realestate-contracting.com'> Savory Housing</a>, All rights reserved.</td>";
                sbMessageBody += "<td style='text-align:right;padding:5px;color:#363636;'>Our mailing address is:<br/><a style='text-decoration:none; color:#036838;' href='mailto:info@realestate-cntracting.com'>info@realestate-cntracting.com</a></td></tr></table></td></tr></table>";

                MailMessage msg = new MailMessage();
                MailAddress fromAddress = new MailAddress(ConfigurationManager.AppSettings["AdminEmail"].ToString(), "Savory Housing.");
                msg.From = fromAddress;
                msg.From = fromAddress;
                MailAddress toAddress = new MailAddress(emailID);
                AlternateView htmlView = AlternateView.CreateAlternateViewFromString(sbMessageBody.ToString(), new System.Net.Mime.ContentType("text/html"));
                msg.AlternateViews.Add(htmlView);
                msg.To.Add(toAddress);
                msg.Subject = subject;
                msg.Body = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"><html><head></head><body leftmargin='0' marginwidth='0' topmargin='0' marginheight='0' offset='0' style='background:#fff; color:#363636; line-height:20px'>" + sbMessageBody + "</body></html>";
                string smtpServer = ConfigurationManager.AppSettings["SMTPServer"];
                string userName = ConfigurationManager.AppSettings["SMTPUserID"];
                string PassWord = ConfigurationManager.AppSettings["SMTPPassword"];
                SmtpClient SmtpClient = new SmtpClient();
                SmtpClient.Host = smtpServer;
                SmtpClient.Credentials = new System.Net.NetworkCredential(userName,PassWord);
                SmtpClient.EnableSsl = true;
                SmtpClient.Send(msg);
                mailSent = true;

            }
            catch (Exception ex)
            {
                throw;
            }

            return mailSent;
        }
    }
}